// qwqScheduledLoggingScoreboardCommand.java
package qwq.zyu.Scoreboard;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;

import java.util.HashMap;
import java.util.Map;

public class qwqScheduledLoggingScoreboardCommand implements CommandExecutor {

    private final Plugin plugin;
    private final Map<String, Boolean> loggingStatus = new HashMap<>();
    private final Map<String, qwqScheduledScoreboardTask> scoreboardTasks = new HashMap<>();

    public qwqScheduledLoggingScoreboardCommand(Plugin plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length != 3) {
            sender.sendMessage("Usage: /qwq_set_logging_scoreboard <scoreboard_name> <true/false> <broadcast_to_chat (true/false)>");
            return false;
        }

        String scoreboardName = args[0];
        try {
            boolean enableLogging = Boolean.parseBoolean(args[1]);
            boolean shouldBroadcast = Boolean.parseBoolean(args[2]);
            setLoggingStatus(sender, scoreboardName, enableLogging, shouldBroadcast);
        } catch (Exception e) {
            sender.sendMessage("An error occurred while processing your request. e:" + e.getMessage());
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private synchronized void setLoggingStatus(CommandSender sender,
                                               String scoreboardName,
                                               boolean enableLogging,
                                               boolean shouldBroadcast) {
        if (enableLogging) {
            if (!loggingStatus.containsKey(scoreboardName) || !loggingStatus.get(scoreboardName)) {
                startLoggingForScoreboard(sender, scoreboardName, shouldBroadcast);
            } else {
                sender.sendMessage("Logging for " + scoreboardName + " is already enabled.");
            }
        } else {
            stopLoggingForScoreboard(sender, scoreboardName);
        }
    }

    private synchronized void startLoggingForScoreboard(CommandSender sender,
                                                        String scoreboardName,
                                                        boolean shouldBroadcast) {
        if (!scoreboardTasks.containsKey(scoreboardName)) {
            qwqScheduledScoreboardTask task = new qwqScheduledScoreboardTask(plugin, scoreboardName, shouldBroadcast);
            task.runTaskTimer(plugin, 0L, 200L); // 每10秒执行一次
            scoreboardTasks.put(scoreboardName, task);
            loggingStatus.put(scoreboardName, true);
            sender.sendMessage("Started logging for " + scoreboardName + ".");
        }
    }

    private synchronized void stopLoggingForScoreboard(CommandSender sender, String scoreboardName) {
        if (scoreboardTasks.containsKey(scoreboardName)) {
            qwqScheduledScoreboardTask task = scoreboardTasks.remove(scoreboardName);
            if (task != null && !task.isCancelled()) {
                task.cancel();
            }
            loggingStatus.put(scoreboardName, false);
            sender.sendMessage("Stopped logging for " + scoreboardName + ".");
        } else {
            sender.sendMessage("No logging task found for " + scoreboardName + ".");
        }
    }

    public synchronized void stopAllLogging() {
        for (Map.Entry<String, qwqScheduledScoreboardTask> entry : scoreboardTasks.entrySet()) {
            qwqScheduledScoreboardTask task = entry.getValue();
            if (task != null && !task.isCancelled()) {
                task.cancel();
            }
        }
        scoreboardTasks.clear();
        loggingStatus.clear();
    }
}